package com.ncloud.act.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.act.common.ActDataHelper;
import com.ncloud.act.entity.ChnlRealBill;
import com.ncloud.act.entity.ChnlSales;
import com.ncloud.act.mapper.BsCycleMapper;
import com.ncloud.act.mapper.ChnlRealBillMapper;
import com.ncloud.act.mapper.ChnlSalesMapper;
import com.ncloud.act.entity.BsCycle;
import com.ncloud.common.exception.UserException;
import com.ncloud.param.service.SequenceService;

@Service
public class ChnlRealBillTransServiceImpl{
	@Resource
	private ChnlSalesMapper chnlSalesMapper;
	@Resource
	private ChnlRealBillMapper chnlRealBillMapper;
	@Resource
	private BsCycleMapper bsCycleMapper;
	@Reference
	private SequenceService sequenceService;
	@Transactional(propagation=Propagation.REQUIRED)
	public JSONObject insertChnlRealBill(JSONObject json) throws ParseException {
		Long bill_id;
		Long chnl_id;
		Long user_id;
		Long order_id;
		Long cust_id;
		Long discnt_inst_id;
		Long fee;
		Long create_user_id;
		if(json.containsKey("userId"))
		{
			user_id=json.getLong("userId");
		}else{
			throw new UserException("4000", "没有用户编码");
		}
		if(json.containsKey("orderId"))
		{
			order_id=json.getLong("orderId");
		}else{
			throw new UserException("4000", "没有订单编码");
		}
		if(json.containsKey("custId"))
		{
			cust_id=json.getLong("custId");
		}else{
			throw new UserException("4000", "没有客户编码");
		}
		if(json.containsKey("discntInstId"))
		{
			discnt_inst_id=json.getLong("discntInstId");
		}else{
			throw new UserException("4000", "没有资费实例编码");
		}
		if(json.containsKey("fee"))
		{
			fee=json.getLong("fee");
		}else{
			throw new UserException("4000", "没有结算金额");
		}
		if(json.containsKey("createUserId"))
		{
			create_user_id=json.getLong("createUserId");
		}else{
			throw new UserException("4000", "没有操作用户编码");
		}
		
		List<ChnlSales> ChnlSales=chnlSalesMapper.getChnlSalesByUserID(user_id);
		if(ChnlSales.size()>0)
		{
			chnl_id=ChnlSales.get(0).getChnl_id();
		}else{
			throw new UserException("4000", "没有对应渠道信息");
		}
		//当前账期
		List<BsCycle> currentCycle = bsCycleMapper.getCurrentCycle();
		if (currentCycle.size()==0){return json;}
		Integer current_cycle_id=currentCycle.get(0).getCycle_id();
		JSONObject param = new JSONObject();
		param.put("seqName", "chnl_bill_id");
		bill_id=sequenceService.getSequence(param);
		if(bill_id==0)	return null;
		ChnlRealBill chnlRealBill=new ChnlRealBill();
		chnlRealBill.setBill_id(bill_id);
		chnlRealBill.setChnl_id(chnl_id);
		chnlRealBill.setUser_id(user_id);
		chnlRealBill.setOrder_id(order_id);
		chnlRealBill.setCust_id(cust_id);
		chnlRealBill.setDiscnt_inst_id(discnt_inst_id);
		chnlRealBill.setCycle_id(current_cycle_id);;
		chnlRealBill.setFee(fee);
		chnlRealBill.setBalance(fee);
		chnlRealBill.setBill_tag("0");//0-未结清 1-已结清 2-部分结清
		chnlRealBill.setCreate_time(new Date());
		chnlRealBill.setCreate_user_id(create_user_id);
		chnlRealBill.setUpdate_time(new Date());
		chnlRealBill.setUpdate_user_id(create_user_id);
		chnlRealBill.setRemark("");
		chnlRealBillMapper.insertChnlRealBill(chnlRealBill);
		return json;
	}
	public JSONArray genChnlRealBill(JSONObject json) {
		Long chnl_id;
		if(json.containsKey("chnl_id"))
		{
			chnl_id=json.getLong("chnl_id");
		}else{
			throw new UserException("4000", "没有渠道编码");
		}
		List<ChnlRealBill> chnlRealBills=chnlRealBillMapper.getChnlRealBillByChnlID(chnl_id);
		JSONArray results=JSON.parseArray(JSON.toJSONString(chnlRealBills));
		ActDataHelper.div100(results, new String[] { "fee","balance"});
		return results;
	}
}
